﻿using System;
using Gp.Scripts.Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Gp.Scripts.GUI {
    public class OptionUIButton : MonoBehaviour {
        public TMP_Text text;
        public Image icon;
        public Button button;


        private Action _onSelect;

        private OptionWindowController _controller;
        public void Init(OptionWindowController controller) {
            _controller = controller;
        }

        
        private void Start() {
            button.onClick.AddListener(() => {
                _onSelect?.Invoke();
                // 通知controller已选中
                _controller.OptionSelected(this);
            });
        }


        public void RenderOption(OptionItem item) {
            text.text = item.Info.text;
            
            icon.sprite = item.Info.icon == null ? _controller.defaultIcon : item.Info.icon;
            
            _onSelect = item.OnSelect;
        }
    }
}